package com.ssyc.common.manager;

import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.text.TextUtils;

import com.blankj.utilcode.util.LogUtils;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.utils.UIHelper;

import java.io.IOException;

/**
 * 封装的MediaPlayer管理类
 * Created by Administrator on 2018/5/11 0011.
 */

public class MediaPlayerManager {

    private static MediaPlayerManager mInstance;

    private MediaPlayer player;

    /**
     * 获取单例引用
     *
     * @return
     */
    public static MediaPlayerManager getInstance() {
        if (mInstance == null) {
            synchronized (MediaPlayerManager.class) {
                if (mInstance == null) {
                    mInstance = new MediaPlayerManager();
                }
            }
        }
        return mInstance;
    }


    /**
     * 播放录音
     *
     * @param url
     * @return
     */
    public boolean play(String url) {
        return play(url, new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer arg0) {
                stop();
            }
        }, new MediaPlayer.OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
                stop();
                return false;
            }
        });
    }

    /**
     * 播放录音
     *
     * @param url
     * @return
     */
    public boolean play(String url, MediaPlayer.OnCompletionListener completionListener, MediaPlayer.OnErrorListener errorListener) {
        stop();
        if (TextUtils.isEmpty(url)) {
            return false;
        }
        try {
            if (player == null) {
                player = new MediaPlayer();
                player.reset();
                player.setDataSource(url);
                player.setVolume(0.6f, 0.6f);
                player.setAudioStreamType(AudioManager.STREAM_MUSIC);

                //同步 会阻塞
                // player.prepare();
                // player.start();

                //异步
                player.prepareAsync();
                player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        player.start();
                    }
                });

                player.setOnCompletionListener(completionListener);
                player.setOnErrorListener(errorListener);
                return true;
            }
        } catch (Exception e) {

            LogUtils.iTag("test", "播放异常" + e);
            stop();
        }
        return false;
    }


    /**
     * 播放录音
     *
     * @param url
     * @return
     */
    public boolean playbar(String url, MediaPlayer.OnCompletionListener completionListener, MediaPlayer.OnErrorListener errorListener) {
        stop();
        if (TextUtils.isEmpty(url)) {
            return false;
        }

        try {
            if (player == null) {
                player = new MediaPlayer();
                player.reset();
                player.setDataSource(url);
                player.setVolume(0.6f, 0.6f);
                player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                //同步:会阻塞
                // player.prepare();
                // player.start();

                //异步
                player.prepareAsync();
                player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        player.start();
                    }
                });

                player.setOnCompletionListener(completionListener);
                player.setOnErrorListener(errorListener);

                player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                    @Override
                    public void onBufferingUpdate(MediaPlayer mp, int percent) {
                        if (percent > 50) {
                            UIHelper.dismissDialog();
                        }
                    }
                });

                return true;
            }
        } catch (Exception e) {
            stop();
        }
        return false;
    }


    /**
     * 播放录音
     *
     * @return
     */
    public boolean playLocalFile(int resId, MediaPlayer.OnCompletionListener completionListener, MediaPlayer.OnErrorListener errorListener) {
        stop();
        if (resId == -1) {
            return false;
        }
        try {
            if (player == null) {
                player = new MediaPlayer();
                player.reset();
                AssetFileDescriptor file = BaseApplication.context.getResources().openRawResourceFd(resId);
                try {
                    player.setDataSource(file.getFileDescriptor(), file.getStartOffset(),
                            file.getLength());
                    player.prepare();
                    file.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                player.setVolume(0.6f, 0.6f);
//                player.setLooping(true);
                player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                player.start();
                player.setOnCompletionListener(completionListener);
                player.setOnErrorListener(errorListener);
                return true;
            }
        } catch (Exception e) {
            stop();
        }
        return false;
    }


    /**
     * 释放资源
     */
    public void stop() {
        if (player != null) {
            try {
                player.stop();
                player.release();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                player = null;
            }
        }
    }
}
